//
//  XMAccessableLabel.m
//  UIMenuControllerTest
//
//  Created by xiaoming on 16/10/26.
//  Copyright © 2016年 xiaoming. All rights reserved.
//

#import "XMAccessableLabel.h"

@implementation XMAccessableLabel {
    UIMenuController *menuController;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.userInteractionEnabled = YES;
    
    UILongPressGestureRecognizer *pan = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    [self addGestureRecognizer:pan];
}

- (void)longAction:(UILongPressGestureRecognizer *)ges {
    UILabel *lab = (UILabel *)ges.view;
    if (!menuController) {
        menuController = [UIMenuController sharedMenuController];

//        只需要实现copy: 和paste:方法即可
//        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copy:)];
//        UIMenuItem *pastItem = [[UIMenuItem alloc] initWithTitle:@"粘贴" action:@selector(paste:)];
        UIMenuItem *speekItem = [[UIMenuItem alloc] initWithTitle:@"阅读" action:@selector(speeckAction)];
        UIMenuItem *logItem = [[UIMenuItem alloc] initWithTitle:@"Log" action:@selector(logAction)];
        menuController.menuItems = @[speekItem, logItem];
    }
    CGSize size = self.intrinsicContentSize;
    CGRect lastRect = CGRectMake(0, self.frame.size.height / 2.0 - size.height / 2.0, self.frame.size.width, size.height);
    lastRect = [lab convertRect:lastRect toView:lab.superview];
    [menuController setTargetRect:lastRect inView:lab.superview];
    [menuController setMenuVisible:YES animated:YES];
    [self becomeFirstResponder];
}

static NSString *const AccessLabelText = @"AccessableLabe";
//  复制
- (void)copy:(id)item {
    UIPasteboard *boarde = [UIPasteboard generalPasteboard];
    [boarde setValue:self.text forPasteboardType:AccessLabelText];
    NSLog(@"copy : %@", [boarde valueForPasteboardType:AccessLabelText]);
    [menuController setMenuVisible:NO animated:YES];
}

//  粘贴
- (void)paste:(id)sender {
    UIPasteboard *boarde = [UIPasteboard generalPasteboard];
    NSData *data = [boarde valueForPasteboardType:AccessLabelText];
    NSString *value = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    self.text = [self.text stringByAppendingString:value];
    NSLog(@"past : %@", value);
    [menuController setMenuVisible:NO animated:YES];
}

//  选择
- (void)select:(id)sender {
    NSLog(@"sender = %@", sender);
}

//  全选
- (void)selectAll:(id)sender {
    [[UIPasteboard generalPasteboard] setValue:self.text forPasteboardType:AccessLabelText];
}

//  删除
- (void)delete:(id)sender {
    self.text = @"";
}

- (void)speeckAction {
    NSLog(@"You must speeck Large Than Before");
    [menuController setMenuVisible:NO animated:YES];
}

- (void)logAction {
    NSLog(@"self = %@, text = %@", self, self.text);
    [menuController setMenuVisible:NO animated:YES];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:)) || (action == @selector(paste:) || (action == @selector(logAction)) || (action == @selector(speeckAction)) || (action == @selector(select:)) || (action == @selector(selectAll:)) || (action == @selector(delete:)));
}

@end
